package com.taihua_tech.basic.util;


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;

/**
 * XML操作工具类
 */
public class XmlUtil {
    public static Document readDocument(String xmlFile){
        Document result = null;
        try {
            SAXReader saxReader = new SAXReader();
            result = saxReader.read(new File(xmlFile));
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
    public static Document readDocument(String xmlFile,boolean checkValidation){
        Document result = null;
        try {
            SAXReader saxReader = new SAXReader();
	        if (!checkValidation){
	            saxReader.setEntityResolver(new NoOpEntityResolver());
		        saxReader.setIncludeExternalDTDDeclarations(false);
		        saxReader.setValidation(false);
	        }
	        result = saxReader.read(new File(xmlFile));
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
    public static Document readDocument(Reader reader){
        Document result = null;
        try {
            SAXReader saxReader = new SAXReader();
            result = saxReader.read(reader);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
    public static Document readDocument(InputStream inputStream){
        Document result = null;
        try {
            SAXReader saxReader = new SAXReader();
            result = saxReader.read(inputStream);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }    
    
    public static Document readDocument(InputStream inputStream,boolean checkValidation){
        Document result = null;
        try {
            SAXReader saxReader = new SAXReader();
	        if (!checkValidation){
	            saxReader.setEntityResolver(new NoOpEntityResolver());
		        saxReader.setIncludeExternalDTDDeclarations(false);
		        saxReader.setValidation(false);
	        }
            result = saxReader.read(inputStream);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    } 
    
    public static Document readDocument(InputStream inputStream,String encoding){
        Document result = null;
        try {
            SAXReader saxReader = new SAXReader();
            saxReader.setEncoding(encoding);
            result = saxReader.read(inputStream);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    } 
    
    public static Document readDocument(InputStream inputStream,String encoding,boolean checkValidation){
        Document result = null;
        try {
            SAXReader saxReader = new SAXReader();
            saxReader.setEncoding(encoding);
	        if (!checkValidation){
	            saxReader.setEntityResolver(new NoOpEntityResolver());
		        saxReader.setIncludeExternalDTDDeclarations(false);
		        saxReader.setValidation(false);
	        }            
            result = saxReader.read(inputStream);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    } 
    
    public static void writeDocument(Document document,String encoding,String outputFile){
        try {
	        OutputFormat format = OutputFormat.createPrettyPrint();
	        OutputStreamWriter fileOutputStream = new OutputStreamWriter(new FileOutputStream(outputFile),encoding);
	        XMLWriter output = new XMLWriter(fileOutputStream,format);
	        output.write(document);
	        output.close();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public static Document createDocument(){
        Document document = DocumentHelper.createDocument();
        return document;
    }
}
class NoOpEntityResolver implements EntityResolver {
	public InputSource resolveEntity(String publicId, String systemId) {
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream("".getBytes());
		return new InputSource(byteArrayInputStream);
//		return new InputSource("./workflow_2_8.dtd");
	}
}
